
Page({
    data: {
         goodsList:[],
         orderName:'李先生',
         orderPhone:'186****3980',
         orderAddress:'北京市昌平区回龙观天通苑一区5号楼三单元' ,
         address:null

        
    },
    onLoad:function(opt){
     var userInfo= wx.getStorageSync('userInfo');
        this.setData({
            userInfo:userInfo,
            orderCode:opt.id
     })

     this.getShopMarket();
    },
    onShow:function(){
       var address= wx.getStorageSync('shopAddress');
       console.log("address")
       console.log(address)
       this.setData({
          address:address
       })
       if (address) {
        this.creatOrder();
       }
       

    },
    getShopMarket:function(){
        var _self=this;
          wx.request({
            url: getApp().globalData.baseUrl + '/pj/order/getDetail?orderCode='+this.data.orderCode,
            method:"GET",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
               _self.setData({
                    goodsList:res.data.obj
              })
                
                                      
             }
            }) 
    },
    chooseGoods:function(e){
     console.log(e.currentTarget.dataset.item);
     console.log(e.currentTarget.dataset.companyid);
     var arr=this.data.goodsList;
     for(var i=0;i<arr.supplierCartVos.length;i++){
        console.log('merchantId='+arr.supplierCartVos[i].merchantId)
         if (arr.supplierCartVos[i].merchantId==e.currentTarget.dataset.companyid) {
            console.log('命中1')
            for(var j=0;j<arr.supplierCartVos[i].supplierCartParts.length;j++){
               
                if (arr.supplierCartVos[i].supplierCartParts[j].type==2) {
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id&&arr.supplierCartVos[i].supplierCartParts[j].normsId==e.currentTarget.dataset.item.normsId) {
                      arr.supplierCartVos[i].supplierCartParts[j].status=arr.supplierCartVos[i].supplierCartParts[j].status=='0'?"1":"0"
                   } 
                }else{
                   if (arr.supplierCartVos[i].supplierCartParts[j].id==e.currentTarget.dataset.item.id) {
                      arr.supplierCartVos[i].supplierCartParts[j].status=arr.supplierCartVos[i].supplierCartParts[j].status=='0'?"1":"0"
                   } 
                }

             
            }
         }
     }
     console.log(arr)
     this.setData({
        goodsList:arr
     })
     this.getMoney();
    },
    getMoney:function(){
            var _self=this;
            var arr=[];
            for(var i=0;i<this.data.goodsList.supplierCartVos.length;i++){
               for(var j=0;j<this.data.goodsList.supplierCartVos[i].supplierCartParts.length;j++){
                     var json={
                        "id":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].id,
                        "merchantId":this.data.goodsList.supplierCartVos[i].merchantId,//供应商ID:"GYS771337000452",
                        "type":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].type,//商品类型：1普通商品 2规格商品,
                        "normsId":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].normsId,//规格ID（如果type==1,就不需要传）,
                        "num":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].num,//数量,
                        "status":this.data.goodsList.supplierCartVos[i].supplierCartParts[j].status//勾选状态：0没勾选 1已勾选
                    }
                   arr.push(json) 
               }  
            }
            console.log(arr)
       
            wx.request({
            url: getApp().globalData.baseUrl + '/pj/shopping/countCart',
            method:"POST",
            data:{
                openid:this.data.userInfo.openid,
                pjCountCartRequestListVo:arr
            },
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
                if (res.data.code==200) {
                 _self.setData({
                    goodsList:res.data.obj
                 })
               }
                               
            }
            }) 
    },
    chooseAddress:function(){
        if(this.data.goodsList.status==1){
              wx.navigateTo({
              url: '/pages/address/address'
             })
        }
       
    },
    creatOrder:function(){
        var _self=this;
        wx.request({
            url: getApp().globalData.baseUrl + '/pj/order/updateAddr',
            method:"POST",
            data:{
                openid:this.data.userInfo.openid,
                addrId:this.data.address.id,
                orderCode:this.data.orderCode
            },
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
               if (res.data.code!=200) {
                  wx.showToast({
                    title: res.data.des,
                    icon: 'none',
                    duration: 2000
                  })
               }
              wx.setStorageSync('shopAddress',null);
               
               _self.setData({
                  address:null
               })
              _self.getShopMarket();
                               
             }
            }) 
    },
    creatOrder2:function(){
      var _self=this;
        wx.request({
            url: getApp().globalData.baseUrl + '/pj/order/toPay?orderCode='+this.data.orderCode,
            method:"GET",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
               console.log(res)
                if (res.data.code==200) {
                    wx.requestPayment({
                      timeStamp: res.data.obj.timeStamp,
                      nonceStr: res.data.obj.nonceStr,
                      package: res.data.obj.package,
                      signType: 'MD5',
                      paySign: res.data.obj.paySign,
                      success (res) { 
                        console.log('成功')
                        console.log(res)
                        _self.getShopMarket();
                      },
                      fail (res) {
                        console.log('失败')
                         _self.getShopMarket();
                       }
                    })
               }
                               
             }
            }) 
    },
    creatOrder3:function(){
        var _self=this;
         wx.showModal({
            title: '提示',
            cancelText:'再想想',
            confirmText:'确认取消',
            content: "取消支付?",
            success (res) {
            if (res.confirm) {
               console.log('用户点击确定')
               wx.request({
            url: getApp().globalData.baseUrl + '/pj/order/updateOrder?orderCode='+_self.data.orderCode,
            method:"POST",
            header:{
               'openid':_self.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            data:{
                orderCode:_self.data.orderCode,
                openid:_self.data.userInfo.openid,
                status:5
            },
            success: function(res) {
               console.log(res)
                _self.getShopMarket();
                if (res.data.code==200) {
                   
               }
                               
             }
            }) 
                 } else if (res.cancel) {
                      console.log('用户点击取消') 
               }
         }
     })



     
        
    },
    creatOrder4:function(){
     

        var _self=this;
         wx.showModal({
            title: '提示',
            cancelText:'取消',
            confirmText:'确认',
            content: "确认收货?",
            success (res) {
            if (res.confirm) {
               console.log('用户点击确定')
               wx.request({
            url: getApp().globalData.baseUrl + '/pj/order/updateOrder?orderCode='+_self.data.orderCode,
            method:"POST",
            header:{
               'openid':_self.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            data:{
                orderCode:_self.data.orderCode,
                openid:_self.data.userInfo.openid,
                status:4
            },
            success: function(res) {
               console.log(res)
                _self.getShopMarket();
                if (res.data.code==200) {
                   
               }
                               
             }
            }) 
                 } else if (res.cancel) {
                      console.log('用户点击取消') 
               }
         }
     })
    },
     callPhone:function(){
      wx.makePhoneCall({
        phoneNumber: '13691122213'//仅为示例，并非真实的电话号码
      })
    },

});